package com.zone.gateway.core.datasource.unpooled;

import com.zone.gateway.core.datasource.Connection;
import com.zone.gateway.core.datasource.DataSource;
import com.zone.gateway.core.datasource.DataSourceType;
import com.zone.gateway.core.datasource.connection.DubboConnection;
import com.zone.gateway.core.mapping.HttpStatement;
import com.zone.gateway.core.session.Configuration;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;

import java.util.Objects;

/**
 * @author: zongzi
 * @description: 未池化的数据源
 * @date: 2025/4/21
 */
public class UnpooledDataSource implements DataSource {
    private DataSourceType dataSourceType;
    private Configuration configuration;
    private HttpStatement httpStatement;

    protected UnpooledDataSource() {

    }

    @Override
    public Connection getConnection() {
        if (Objects.requireNonNull(dataSourceType) == DataSourceType.DUBBO) {
            // 接口全限定类名
            String interfaceName = httpStatement.getInterfaceName();
            // rpc 泛化引用
            ReferenceConfig<GenericService> referenceConfig = configuration.getReferenceConfig(interfaceName);
            return new DubboConnection(referenceConfig);
        }
        throw new RuntimeException("unknow data source type: " + dataSourceType);
    }

    public void setDataSourceType(DataSourceType dataSourceType) {
        this.dataSourceType = dataSourceType;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public void setHttpStatement(HttpStatement httpStatement) {
        this.httpStatement = httpStatement;
    }
}
